const Router = require('koa-router')
const Goods = require('../model/goods.js')
const goods = new Router()


goods.get('/goodslist', async (ctx, next) => {
	let type = ctx.query.type // 商品类型
	let pageIndex = ctx.query.pageIndex || 1 // 当前页
	let pageSize = parseInt(ctx.query.pageSize || 10) // 每页数量
	//let pageCount = await Goods.find().countDocuments() // 总条数
	let pageSkip = (pageIndex - 1) * pageSize
	try {
		let res = await Goods.find({
			goodsType: type
		}).skip(pageSkip).limit(pageSize)
		ctx.body = {
			code: 2,
			data: res
		}
	} catch (e) {
		ctx.body = {
			code: 0,
			msg: '暂无数据'
		}
	}
})
goods.get('/show', async (ctx, next) => {
	try {
		let id = ctx.query.id
		let res = await Goods.findOne({
			_id: id
		})
		ctx.body = {
			code: 2,
			data: res
		}
	} catch (e) {
		ctx.body = {
			code: 0,
			msg: '该商品不存在'
		}
	}
})
module.exports = goods
